--[[ 监视地址数值用脚本,改变监视地址需手动修改GAME.ADDRS
  
  指定地址数组{},每一项格式为{地址,数据名称},效果是监视该地址数值
  
  By setycyas @2025-11-15
]]

--防止重复运行
if (_G.game) then
  print('script already loaded!')
  return
end

local myMame = require('MyMame')
local myStg = require('MySTG')

-- 这里设定基本参数
local GAME = {
  -- 脚本名,防重加载用
  NAME = "MyAddrWatcher_autoboot",
  -- 监视的地址数组
  ADDRS = {
    {0x108D1D, "1P_lives"},
  },
  -- mame api用的对象
  mem = manager.machine.devices[':maincpu'].spaces['program'],
  container = manager.machine.screens[":screen"].container,
  ui_container = manager.machine.render.ui_container,
}
-- 更新一下模块的参数
myMame.gameName = GAME.NAME
myMame.screenIndex = ':screen'
myStg.screenIndex = ':screen'

--[[ **************************
  主函数,绑定后每一帧执行一次该函数
]]
function GAME.Main()
  local i, item, addr, paramName, param
  local count = #GAME.ADDRS
  
  -- 显示数据
  for i = 1, count do
    item = GAME.ADDRS[i]
    addr = string.format("%x", item[1])
    paramName = item[2]
    param = GAME.mem:read_u8(item[1])
    GAME.ui_container:draw_text(
      0.05, 0.05*i, 
      string.format("%s - %s : %d", addr, paramName, param), 
      0xFF000000, 0xFF839496
    )
  end
  
end

--[[运行]]

-- 绑定每一帧执行的函数,测试时绑定到debug
myMame.debugRunPerFrame(GAME.Main, 'MyAddrWatcher')
--myMame.runPerFrame(GAME.Main, 'MyAddrWatcher')
-- 导出到全局变量
_G.game = GAME

return GAME
